package com.xzs.thinking.controller;

import com.xzs.thinking.model.HelloWorldEsVO;
import com.xzs.thinking.model.HelloWorldVO;
import com.xzs.thinking.service.HelloWorldService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author : Iris.Xiao
 * @date : 2017/11/17 16:31
 * @description :
 */
@RestController
public class HelloWorldController {

    @Autowired
    private HelloWorldService helloWorldService;

    @ApiOperation(value="方法简要说明", notes="方法具体注释")
    @RequestMapping(value="/mocktest",method ={RequestMethod.GET,RequestMethod.POST})
    public String mocktest(
            @ApiParam(required=true,name = "word",value = "参数说明")
            @RequestParam(name="word")String word){
        return "Hello world!"+word;
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping("/")
    public String hello(){
        return "Hello world!";
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping(value = "/exceptionTest",method ={RequestMethod.GET,RequestMethod.POST})
    public HelloWorldVO exceptionTest(){
        HelloWorldVO vo = new HelloWorldVO();

        try {
            helloWorldService.exceptionTest();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }
    @RequestMapping(value = "/dbtest",method ={RequestMethod.GET,RequestMethod.POST})
    public HelloWorldVO dbtest(){
        return helloWorldService.getVo("1");
    }

    @RequestMapping(value = "/getVoByMybatisDev",method ={RequestMethod.GET,RequestMethod.POST})
    public HelloWorldVO getVoByMybatisDev(){
        return helloWorldService.getVoByMybatisDev("1");
    }

    @RequestMapping(value = "/getVoByMybatisUat",method ={RequestMethod.GET,RequestMethod.POST})
    public HelloWorldVO getVoByMybatisUat(){
        return helloWorldService.getVoByMybatisUat("1");
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping("/updateDev")
    public void updateDev(){
        HelloWorldVO helloWorldVO = new HelloWorldVO();
        helloWorldVO.setArticleId("1");
        helloWorldVO.setClickCount(Long.valueOf(123));
        helloWorldService.updateDev(helloWorldVO);
    }
    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping("/updateUat")
    public void updateUat(){
        HelloWorldVO helloWorldVO = new HelloWorldVO();
        helloWorldVO.setArticleId("1");
        helloWorldVO.setClickCount(Long.valueOf(123));
        helloWorldService.updateUat(helloWorldVO);
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping("/redisSave")
    public void redisSave(){
        HelloWorldVO helloWorldVO = helloWorldService.getVo("1");
        helloWorldService.save(helloWorldVO);
    }
    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping("/redisGet")
    public String redisGet(){
        return helloWorldService.get("1");
    }

    @ApiIgnore//使用该注解忽略这个API
    @RequestMapping("/taskTest")
    public String taskTest() throws Exception{
        helloWorldService.doTaskOne();
        helloWorldService.doTaskTwo();
        helloWorldService.doTaskThree();
        return"taskTest";
    }



    @ApiOperation(value="保存到ES测试方法", notes="保存到ES测试方法")
    @RequestMapping(value = "/saveEs",method ={RequestMethod.GET,RequestMethod.POST})
    public HelloWorldEsVO saveEs(
            @ApiParam(required=true,name = "title",value = "标题")
            @RequestParam String title,
            @ApiParam(required=true,name = "content",value = "内容")
            @RequestParam String content) throws Exception{
        HelloWorldEsVO vo = new HelloWorldEsVO();
        vo.setArticleId("articleId_"+title);
        vo.setContent(content);
        vo.setCreateTime(new Timestamp(System.currentTimeMillis()));
        vo.setContentType(2);
        vo.setTitle(title);
        helloWorldService.saveEs(vo);
        return vo;
    }


    @ApiOperation(value="搜索ES测试方法", notes="搜索ES测试方法")
    @RequestMapping(value = "/searchEs",method ={RequestMethod.GET,RequestMethod.POST})
    public List<HelloWorldEsVO> searchEs(
            @ApiParam(required=true,name = "word",value = "搜索内容")
            @RequestParam String word,
            @ApiParam(required=true,name = "pageNumber",value = "页码")
            @RequestParam Integer pageNumber) throws Exception{

        List<HelloWorldEsVO> result = helloWorldService.searchHelloWorldEsVO(pageNumber,3,word);
        return result;
    }
}
